<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use PDO;
use Think\Exception;
use app\admin\model\Goods as GoodsModel;

class Goods extends Api
{
    protected $noNeedRight = '*';
    protected $noNeedLogin = ['getLists','getInfo'];


    public function getLists()
    {
        $cate_id = $this->request->post('cate_id', 0);
        $where = [];
        if($cate_id > 0)
        {
            $where['category_id'] = $cate_id;
        }
        $goods_name = $this->request->post('goods_name','');
        if($goods_name)
        {
            $where['goods_name'] = ['like',"%$goods_name%"];
        }
        $page = $this->request->post("page",1);
        $start = 0;
        if($page > 1)
        {
           $start = ($page - 1)*15;
        }
         //获取商品列表
        $goods_list = GoodsModel::where($where)->field('id as goods_id,goods_name,pirce as price,good_images,v_pirce as v_price,sell')->order('sort desc')->limit($start,65)->select();
        foreach ($goods_list as $k => $v) {
            $image_list = explode(',',$v['good_images']);
            $goods_list[$k]['good_images'] = cdnurl($image_list[0], true);

        }
        $return['goods_list'] = $goods_list;
        $this->success('查询成功', $return);
    }
    
    
    public function getInfo()
    {
        $goods_id = $this->request->post('goods_id', 0);
        if($goods_id == 0)
        {
            $this->error('请选择需要查看的出现',[]);
        }
        $where['id'] = $goods_id;
        $goods_info = GoodsModel::where($where)->field('fx_pirce,id as goods_id,goods_desc,goods_name,pirce as price,good_images,v_pirce as v_price,sell,gooddetails')->find();
        if(!$goods_info)
        {
            $this->error('出行不存在，请重试',[]);
        }
        $fx_price = $goods_info['fx_pirce'];
        unset( $goods_info['fx_pirce']);
        $goods_info['fx_price']  = '';
        if($this->auth->id)
        {
            if($this->auth->is_agent == 1)
            {
                $goods_info['fx_price'] = $fx_price;
            }
        }
        $image_list = explode(',',$goods_info['good_images']);
        $goods_info['good_images'] = $image_list;
        $return['goods_info'] = $goods_info;
        $this->success('查询成功', $return);
    }
    
    
}